import KZJRequest from '../index'
import type {
  TLoginResult,
  TPhone,
  TLoginErCode,
  TLoginErCodeImg,
  TLoginErCodeRequest
} from './type'
import { TRequestDefaultResult } from '../type'

enum LoginApi {
  PhoneLogin = '/login/cellphone', // 手机号密码登录
  SentCode = '/captcha/sent', // 发送验证码
  VerifyCode = '/captcha/verify', //验证验证码
  Logout = '/logout', // 退出登录
  Status = '/login/status', // 刷新登录状态
  SongsList = 'user/playlist', // 获取用户创建歌单
  CodeKey = '/login/qr/key', // 获取二维码key
  CodeCreate = '/login/qr/create', // 创建二维码
  CodeState = '/login/qr/check' //检查二维码状态
}

export function phoneLogin(phone: TPhone) {
  return KZJRequest.post<TLoginResult>({
    url: LoginApi.PhoneLogin,
    params: {
      timestamp: 0
    },
    data: {
      phone: phone.cellphone,
      password: phone.password ?? null,
      captcha: phone.code ?? null
    },
    showLoading: true,
    immediately: true
  })
}

export function phoneSentCode(phone: number | null) {
  return KZJRequest.get<TRequestDefaultResult>({
    url: LoginApi.SentCode,
    params: { phone }
  })
}

export function getUserPlayList(uid: number) {
  return KZJRequest.get<TRequestDefaultResult>({
    url: LoginApi.SongsList,
    params: { uid }
  })
}

export function phoneVerifyCode(phone: number | null, captcha: number | null) {
  return KZJRequest.post<TRequestDefaultResult>({
    url: LoginApi.VerifyCode,
    data: { phone, captcha }
  })
}

export function phoneLogout() {
  return KZJRequest.get<TRequestDefaultResult>({
    url: LoginApi.Logout
  })
}

export function loginStatus(cookie = '') {
  return KZJRequest.post<TRequestDefaultResult>({
    url: LoginApi.Status,
    data: {
      cookie
    },
    immediately: true
  })
}

export function loginErcodeGetKye() {
  return KZJRequest.get<TLoginErCode>({
    url: LoginApi.CodeKey,
    immediately: true,
    takeCookie: false
  })
}

export function loginErcodeCreate(key: string) {
  return KZJRequest.get<TLoginErCodeImg>({
    url: LoginApi.CodeCreate,
    params: { key, qrimg: true },
    immediately: true
  })
}

export function loginErcodeCheckState(key: string) {
  return KZJRequest.get<TLoginErCodeRequest>({
    url: LoginApi.CodeState,
    params: { key },
    immediately: true,
    takeCookie: true
  })
}
